package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.domain.Permission;
import cn.wolfcode.ssm.domain.Role;
import cn.wolfcode.ssm.page.JsonPage;
import cn.wolfcode.ssm.page.PageResult;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IPermissionService;
import cn.wolfcode.ssm.service.IRoleService;
import cn.wolfcode.ssm.util.requiredPermission;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")

public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @requiredPermission(name="员工列表",expression = "XXX:list")
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageResult<Role> pageResult = roleService.query(qo);
        model.addAttribute("pageResult",pageResult);
        return "/role/list";
    }

    @requiredPermission(name="员工删除",expression = "XXX:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            roleService.delete(id);
        }
        return "redirect:/role/list.do";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id){

        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        if (id != null){
            Role role = roleService.get(id);
            model.addAttribute("role",role);
        }
        return "/role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role, Long[] ids){
        if (role.getId() != null){
            roleService.update(role,ids);
        }else{
            roleService.save(role,ids);
        }
        return "redirect:/role/list.do";
    }


}
